/**
 * 
 */

package org.minuframework.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @deception  TODO
 * @author     米mi
 * @date       2018年3月18日下午3:46:12
 * @version    1.0
 * @deprecated
 */
public class FileReader {
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // constants
    
    public static final String UTF8 = "UTF-8";
    
    // ************************************************************************
    // public methods
    
    /**
     * 读取文件
     * @param  file 文件地址 File
     * @return      Properties
     */
    public static Properties reader(File file) {
        Properties p = new Properties();
        
        InputStreamReader reader = null;
        try {
            InputStream in = new FileInputStream(file);
            reader = new InputStreamReader(in, UTF8);
            p.load(reader);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return p;
    }
    
    /**
     * 读取文件
     * @param  file 文件地址 File
     * @return      List<String>
     */
    public static List<String> asList(File file) {
        List<String> result = new ArrayList<String>();
        
        BufferedReader buf = null;
        try {
            InputStream in = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(in, UTF8);
            buf = new BufferedReader(reader);
            
            String read;
            while ((read = buf.readLine()) != null) {
                result.add(read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (buf != null) {
                    buf.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return result;
    }
    
    /**
     * 读取文件
     * @param  file 文件地址 File
     * @return      String[]
     */
    public static String[] asArray(File file) {
        List<String> result = Lists.newArrayList();
        
        BufferedReader buf = null;
        try {
            InputStream in = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(in, UTF8);
            buf = new BufferedReader(reader);
            
            String read;
            while ((read = buf.readLine()) != null) {
                result.add(read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (buf != null) {
                    buf.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return result.toArray(new String[result.size()]);
    }
    
    /**
     * 读取文件
     * @param  file 文件地址 File
     * @return      Map<Object, Object>
     */
    public static Map<Object, Object> asMap(File file) {
        Map<Object, Object> params = Maps.newHashMap();
        Properties propertiesFile = reader(file);
        for (Entry<Object, Object> entry : propertiesFile.entrySet()) {
            params.put(entry.getKey(), entry.getValue());
        }
        return params;
    }
    
    /**
     * 读取文件
     * @param  file   文件地址 File
     * @param  prefix 前缀 String
     * @return        Map<Object, Object>
     */
    public static Map<Object, Object> asMap(File file, String prefix) {
        Map<Object, Object> params = Maps.newHashMap();
        Properties propertiesFile = reader(file);
        for (Entry<Object, Object> entry : propertiesFile.entrySet()) {
            if (entry.getKey().toString().startsWith(prefix)) {
                params.put(entry.getKey(), entry.getValue());
            }
        }
        return params;
    }
    
}
